package com.jnu.ilearn;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.lights.LightsManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jnu.ilearn.bean.User;
import com.jnu.ilearn.utils.DataLoader;
import com.jnu.ilearn.utils.UserUtils;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterActivity extends AppCompatActivity {
    DataLoader dataLoader=new DataLoader();
    List<User> users=new ArrayList<>();
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        EditText NameText=findViewById(R.id.name_input);
        EditText AccountText=findViewById(R.id.account_input);
        EditText PasswordText=findViewById(R.id.password_input);
        EditText PasswordTwiceText=findViewById(R.id.password_input_twice);
        TextView registerView=findViewById(R.id.btn_register);

        getSupportActionBar().hide();
        ImageView backView=findViewById(R.id.back);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() //确认框，点击确定传回数据给主界面
        {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //将控件与变量绑定，用于获取用户输入的数据
                RadioGroup radioGroup=(RadioGroup) findViewById(R.id.check_kind);
                int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
                UserUtils userUtils=new UserUtils();
                String name=NameText.getText().toString();
                String Account=AccountText.getText().toString();
                String Password=PasswordText.getText().toString();
                if(checkedRadioButtonId==R.id.account_manager) //如果选择管理员
                {
                     handler=new Handler(Looper.getMainLooper()){
                        @Override
                        public void handleMessage(@NonNull Message msg) {
                            super.handleMessage(msg);
                            if(userUtils.isInsert()){
                                Intent intent=new Intent();
                                intent.putExtra("account",Account);
                                intent.putExtra("password",Password);
                                intent.putExtra("kind","manager");
                                setResult(1,intent);
                                finish();
                            }
                            else{
                                new AlertDialog.Builder(RegisterActivity.this).setTitle("警告").setMessage("注册失败").show();
                            }
                        }
                    };
                    userUtils.addUser(handler,Account,Password,name,"manager");
                }
                else//注册为学生
                {
                     handler=new Handler(Looper.getMainLooper()){
                        @Override
                        public void handleMessage(@NonNull Message msg) {
                            super.handleMessage(msg);
                            if(userUtils.isInsert()){
                                Intent intent=new Intent();
                                intent.putExtra("account",Account);
                                intent.putExtra("password",Password);
                                intent.putExtra("kind","student");
                                setResult(1,intent);
                                finish();
                            }
                            else{
                                new AlertDialog.Builder(RegisterActivity.this).setTitle("警告").setMessage("注册失败").show();
                            }
                        }
                    };
                    userUtils.addUser(handler,Account,Password,name,"student");
                }

            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        registerView.setOnClickListener(new View.OnClickListener() {//提交注册按钮响应事件
            @Override
            public void onClick(View v) {
                //将控件与变量绑定，用于获取用户输入的数据
                Handler handler=new Handler(Looper.getMainLooper()){
                    @Override
                    public void handleMessage(@NonNull Message msg) {
                        super.handleMessage(msg);
                        users=dataLoader.GetUser();
                        String name=NameText.getText().toString();
                        String Account=AccountText.getText().toString();
                        String Password=PasswordText.getText().toString();
                        String PasswordTwice=PasswordTwiceText.getText().toString();
                        boolean repeat=false;
                        String Regex="^[0-9a-z]+\\w*@([0-9a-z]+\\.)+[0-9a-z]+$";
                        Pattern compile = Pattern.compile(Regex);
                        Matcher matcher = compile.matcher(Account);
                        for (User user : users) {
                            if(Account.equals(user.getAccount())){
                                repeat=true;
                                break;
                            }
                        }
                        if(name.length()>10){
                            new AlertDialog.Builder(RegisterActivity.this).setTitle("警告").setMessage("昵称位数不得超过10位").show();
                        }
                        else if(!matcher.matches()){
                            new AlertDialog.Builder(RegisterActivity.this).setTitle("警告").setMessage("邮箱格式不正确").show();
                        }
                        else if(Account.length()>20){
                            new AlertDialog.Builder(RegisterActivity.this).setTitle("警告").setMessage("账号字符不得多于20位").show();
                        }
                        else if(repeat){
                            new AlertDialog.Builder(RegisterActivity.this).setTitle("警告").setMessage("此邮箱已被注册").show();
                        }
                        else if(!Password.equals(PasswordTwice)){
                            new AlertDialog.Builder(RegisterActivity.this).setTitle("警告").setMessage("两次密码不一致，请重新输入").show();
                        }
                        else{
                            //将确认框显示出来
                            builder.setMessage("是否确定以此邮箱与密码进行注册");
                            builder.setTitle("提示");
                            builder.show();
                        }
                    }
                };
                dataLoader.LoadUser(handler);


            }
        });
    }
}